#include <stdio.h>
#include <stdlib.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <string.h>
#include "policy.h"

static char* str;
int i = 0;
char ** policy(int recAppID)
{
  char desInt[10][10];
  xmlNode *cur_node, *child_node, *child_node2;
  xmlChar *costValue, *bandwidthValue;
  char* defInterface="eth0";
//  char* str;
  char *stringappID, *DisplayXType, *altProp, *textFont;
  int appID;	

  // --------------------------------------------------------------------------
  // Open XML document
  // --------------------------------------------------------------------------
 
  xmlDocPtr doc;
  doc = xmlReadFile("/home/sabari/courseworks/SINE/middleware/mptcp_userland_0.1/src/demo/middleware/policyMgr/mypolicy.xml", NULL, 0);
  //desInt[0]=defInterface;
  if (doc == NULL) 
        printf("error: could not parse file file.xml\n");
  printf("OK\n");  
  // --------------------------------------------------------------------------
  // XML root.
  // --------------------------------------------------------------------------

  /*Get the root element node */
  xmlNode *root = NULL;
  root = xmlDocGetRootElement(doc);
  
  // --------------------------------------------------------------------------
  // Must have root element, a name and the name must be "AppConfigData"
  // --------------------------------------------------------------------------
  
 /* if( !root || 
      !root->name ||
      xmlStrcmp(root->name,"AppConfigData") ) 
  {
	printf("FALSE");
     xmlFreeDoc(doc);
     return 0;
  }*/
 // if(!root) return 0; 
	
    if(root==NULL)
	printf("Problem with XML\n");

  // --------------------------------------------------------------------------
  // AppConfigData children: For each DisplayX
  // --------------------------------------------------------------------------
  for(cur_node = root->children; cur_node != NULL; cur_node = cur_node->next)
  {
     if ( cur_node->type == XML_ELEMENT_NODE  &&
          !xmlStrcmp(cur_node->name, (const xmlChar *) "policy" ) )
     {
       stringappID = xmlGetProp(cur_node,"name");
	appID = atoi(stringappID);
	if(recAppID != appID)
	{
		continue;
	}
        for(child_node = cur_node->children; child_node != NULL; child_node = child_node->next)
        {
           if ( cur_node->type == XML_ELEMENT_NODE  &&
                !xmlStrcmp(child_node->name, (const xmlChar *)"interfaceSet") )
           {
           	
		for(child_node2 = child_node->children; child_node2 != NULL; child_node2 = child_node2->next)
		{
			char * str1 = (char*) xmlNodeGetContent(child_node2); 
			desInt[i] = malloc(strlen(str1) * sizeof(char));
			strcpy(desInt[i],str1);
			//memcpy(desInt[i], (char*)xmlNodeGetContent(child_node2),10);			
			printf("ya - %s\n",desInt[i]);
			i++;
		}
		
	   }	

        }
    }
	}

  xmlFreeDoc(doc);
  xmlCleanupParser();
 return desInt; 
}
